package com.bjypc.youth.service.chart;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * Create a BarChart named 不同性别北京青年的价值取向类型
 * 
 * @author chenzwei
 * 
 */
public class SexValueOrientBarChartService {
	private CategoryDataset categoryDataset;

	public SexValueOrientBarChartService() {
	}

	/**
	 * Create a BarChart named 不同性别北京青年的价值取向类型
	 * 
	 * @return
	 */
	public JFreeChart createChart() {
		this.categoryDataset = createDataset();
		JFreeChart jfreechart = ChartFactory.createBarChart("不同性别北京青年的价值取向类型",
				"价值取向类型", "价值取向值", this.categoryDataset,
				PlotOrientation.VERTICAL, true, true, false);
		jfreechart.setBackgroundPaint(Color.white);
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		categoryplot.setBackgroundPaint(Color.lightGray);
		//背景表格线
		categoryplot.setDomainGridlinePaint(Color.white);
		categoryplot.setDomainGridlinesVisible(true);
		categoryplot.setRangeGridlinePaint(Color.white);
		NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// 设置渲染
		BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
		barrenderer.setDrawBarOutline(false);
		GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
				0.0F, 0.0F, new Color(0, 10, 64));
		GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F,
				Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
		GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red,
				0.0F, 0.0F, new Color(64, 0, 0));
		// 设置每个柱的颜色
		barrenderer.setSeriesPaint(0, gradientpaint);
		barrenderer.setSeriesPaint(1, gradientpaint1);
		barrenderer.setSeriesPaint(2, gradientpaint2);
		// 显示每个柱的数值，并修改该数值的字体属性
		barrenderer
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		barrenderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 12));
		barrenderer.setItemLabelsVisible(true);

		// x轴
		CategoryAxis categoryaxis = categoryplot.getDomainAxis();
		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(0.52359877559829882D));
		categoryaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		// ValueAxis类，设置柱到图上下边的距离。
		ValueAxis valueAxis = categoryplot.getRangeAxis();
		//设置最高的一个柱与图片顶端的距离
		valueAxis.setUpperMargin(0.25);
		
		return jfreechart;
	}

	/**
	 * Create a Category Dataset
	 * 
	 * @return
	 */
	private CategoryDataset createDataset() {
		String s = "男";
		String s1 = "女";
		String cat1 = "自我关注取向";
		String cat2 = "公正平等取向";
		String cat3 = "忠信狂荣取向";
		String cat4 = "目的效率取向";
		DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
		defaultcategorydataset.addValue(15.0D, s, cat1);
		defaultcategorydataset.addValue(19.2D, s, cat2);
		defaultcategorydataset.addValue(25.7D, s, cat3);
		defaultcategorydataset.addValue(28.4D, s, cat4);
		defaultcategorydataset.addValue(27.2D, s1, cat1);
		defaultcategorydataset.addValue(27.3D, s1, cat2);
		defaultcategorydataset.addValue(32.0D, s1, cat3);
		defaultcategorydataset.addValue(33.0D, s1, cat4);
		return defaultcategorydataset;
	}
}
